package com.wythe.mall.adapter;

import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.wythe.mall.R;

import java.util.ArrayList;

public class SearchKeyListAdapter extends RecyclerView.Adapter<SearchKeyListAdapter.ViewHolder> {

    private final ArrayList<String> arrayList;
    private OnItemClickListener onItemClickListener;

    public SearchKeyListAdapter(ArrayList<String> arrayList) {
        this.arrayList = arrayList;
        this.onItemClickListener = null;
    }

    @Override
    public int getItemCount() {

        return arrayList.size();

    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        final int positionInt = position;
        final String key = arrayList.get(position);

        holder.mKeyword.setText(key);

        holder.mainLinearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onItemClickListener != null) {
                    onItemClickListener.onClick(positionInt, key);
                }
            }
        });

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup group, int viewType) {
        View view = LayoutInflater.from(group.getContext()).inflate(R.layout.item_list_search_key, group, false);
        return new ViewHolder(view);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {

        this.onItemClickListener = listener;

    }

    public interface OnItemClickListener {

        void onClick(int position, String key);

    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private AppCompatTextView mKeyword;
        private LinearLayoutCompat mainLinearLayout;
        private ViewHolder(View view) {
            super(view);
            mKeyword = view.findViewById(R.id.nameTextView);
            mainLinearLayout = view.findViewById(R.id.mainLinearLayout);
        }

    }

}
